package com.example.demo.tools;

import org.apache.commons.codec.digest.DigestUtils;

/**
 * Creat with IntelliJ IDEA
 * Description:
 * User:WZW
 * Date:2022-08-30
 * Time:22:11
 */
public class MD5Util {

    //定义一个固定的盐值
    private static final String salt = "1f2a3n4g";

    public static String md5(String src) {
        return DigestUtils.md5Hex(src);
    }

    //第一次加密：模拟前端第一次加密，然后传到后端
    public static String inputPassToFormPass(String inputPass) {
        String str = "" + salt.charAt(1) + salt.charAt(3) + inputPass
                + salt.charAt(5) + salt.charAt(6);
        return md5(str);
    }

    /**
     * 第二次加密
     * @param formPass 前端加密过的密码，传给后端进行第二次加密
     * @param salt 用户数据库当中的盐值
     * @return
     */
    public static String formPassToDBPass(String formPass, String salt) {
        String str = "" + salt.charAt(0) + salt.charAt(2) + formPass
                + salt.charAt(5) + salt.charAt(4);
        return md5(str);
    }

    public static String inputPassToDBPass(String inputPass, String saltDB) {
        String formPass = inputPassToFormPass(inputPass);
        String dbPass = formPassToDBPass(formPass,saltDB);
        return dbPass;
    }


}
